<template>
  <div>
    <!-- <h3>订单管理-订单列表</h3> -->
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表 </el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
           <el-input placeholder="请输入内容" v-model="orderList.query" @clear="getOrderList">
            <template #append>
              <el-button icon="el-icon-search" @click="getOrderList"></el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表单项 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="80px"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="80px">
          <template v-slot="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="80px">
          <template v-slot="scope">
            {{ scope.row.is_send }}
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="160px">
          <template v-slot="scope">
            {{ timeFilter(scope.row.create_time) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" v-model="scope.row" size="mini" @click="showBox"></el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgress"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>

      <!-- 修改对话框 -->
      <el-dialog
        title="修改地址"
        v-model="addDialogVisible"
        width="50%"
        @close="addDiologClose">
        <el-form :model="DialogRuleForm" :rules="Dialogrules" ref="DialogRef" label-width="100px" >
          <el-form-item label="省市县/区" prop="Cityrules">
            <el-cascader
                style="width:100%"
                v-model="DialogRuleForm.addCityVal1"
                :options="cityDate"
                :props="props"
                @change="handleChange">
            </el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="Cityrules2">
            <el-input v-model="DialogRuleForm.addCityVal2"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
          </span>
        </template>
      </el-dialog>

      <!-- 时间线对话框 -->
      <el-dialog title="物流信息" v-model="addDiologProgress" width="50%">
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityDate from '../../assets/citydata'
export default {
  data() {
    return {
      // 发送的数据
      queryInfo: {
        // 搜索栏双向数据绑定
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10,
        // 用户 ID
        user_id: '',
        // 支付状态
        pay_status: '',
        // 是否发货
        is_send: '',
        // ['个人','公司']
        order_fapiao_title: '',
        // 公司名称
        order_fapiao_company: '',
        // 发票内容
        order_fapiao_content: '',
        // 发货地址
        consignee_addr: ''
      },
      total: 0,
      // 接收的表单数据
      orderList: [],
      // 地址数据，由 citydata.js 获取
      cityDate,
      // 修改对话框打开关闭布尔值
      addDialogVisible: false,
      // 修改对话框数据绑定
      DialogRuleForm: {
        addCityVal1: [],
        addCityVal2: ''
      },
      // 表单规则
      Dialogrules: {
        Cityrules: [
          { required: true, message: '请选择省市县/区', trigger: 'blur' }
        ],
        Cityrules2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      props: { expandTrigger: 'hover' },
      // 物流对话框
      addDiologProgress: false,
      // 物流信息
      progressInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单信息
    async getOrderList() {
      const { data: res } = await this.$axios.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error(res.mete.msg)
      }
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    // 时间过滤器 方法或计算属性
    timeFilter(originVal) {
      const dt = new Date(originVal)
      // 年月日，时分秒
      const y = dt.getFullYear()
      const m = (dt.getMonth() + 1 + '').padStart(2, '0')
      // padStart()和padStart()一共接受两个参数，第一个参数用来指定字符串的最小长度，第二个参数是用来补全的字符串。
      // 'x'.padStart(5, 'ab') => 'ababx'
      const d = (dt.getDate() + '').padStart(2, '0')
      const hh = (dt.getHours() + '').padStart(2, '0')
      const mm = (dt.getMinutes() + '').padStart(2, '0')
      const ss = (dt.getSeconds() + '').padStart(2, '0')
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    },
    // 监听页码值的修改
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听页面展示条数的修改
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 打开对话框
    showBox() {
      this.addDialogVisible = true
    },
    handleChange() {
      console.log(this.DialogRuleForm)
    },
    // 清空表单规则
    addDiologClose() {
      this.$refs.DialogRef.resetFields()
    },
    // 打开物流对话框
    async showProgress() {
      const { data: res } = await this.$axios.get('/kuaidi/1106975712662')
      console.log(res.data)
      this.progressInfo = res.data
      this.addDiologProgress = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}

</style>
